import cryptoJs from 'crypto-js';

const KEY = 'qWeRt4u7';

// DES加密(message为json字符串)
export const encryptDes = (message, key = KEY) => {
    var keyHex = cryptoJs.enc.Utf8.parse(key)
    var option = {mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7}
    var encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
    return encrypted.toString()
}

export const decryptDes = (word, key = KEY) => {
    // 过滤换行符
    word = word.replace(/[\r\n]/g,"")
    const keyHex = cryptoJs.enc.Utf8.parse(key);
    const decrypted = cryptoJs.DES.decrypt({
        ciphertext: cryptoJs.enc.Base64.parse(word)
    }, keyHex, {
        mode: cryptoJs.mode.ECB,
        padding: cryptoJs.pad.Pkcs7
    });
    return JSON.parse(decrypted.toString(cryptoJs.enc.Utf8))
}
